package edu.zjut.sjq.controller;

import edu.zjut.sjq.bean.Article;
import edu.zjut.sjq.bean.Result;
import edu.zjut.sjq.bean.User;
import edu.zjut.sjq.service.ArticleService;
import edu.zjut.sjq.service.UserService;
import edu.zjut.sjq.util.FileUtil;
import edu.zjut.sjq.util.PathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;

/**
 * Created by JamesBond on 2017/5/20.
 */
@Controller
public class UploadController {

    @Autowired
    private UserService userService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private FileUtil fileUtil;
    @Autowired
    private HttpSession session;

    // 上传头像
    @RequestMapping(value = "/uploadImg",produces = "application/json; charset=utf-8")
    @ResponseBody
    public Result uploadImg(HttpServletRequest request, HttpServletResponse response){
        Result result=new Result();
        result.setRes(0);
        User user = (User) session.getAttribute("user");
        if (user==null){
            result.setMsg("您好，请先登录");
            return result;
        }

        String newName=fileUtil.uploadImg(request, response);
        if (newName!=null){
            User newUser=new User();
            newUser.setId(user.getId());
            newUser.setImgUrl(PathUtil.accessImgUrl+newName);
            return userService.updateInfo(newUser);
        }else
            result.setMsg("上传出错");
        return result;
    }

    /**
     * 显示头像
     * @param filename 文件名
     */
    @RequestMapping(value="/img/{filename:.+}", method = RequestMethod.GET)
    public void showImg(@PathVariable("filename")String filename,
                             HttpServletRequest request, HttpServletResponse response){

        fileUtil.showImg(filename,request, response);
    }


    // 上传作品
    @RequestMapping(value = "/uploadArticle",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Result uploadArticle(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
      /*  request.setCharacterEncoding("UTF-8");
        String title = request.getParameter("title");
        Enumeration<String> names = request.getParameterNames();
        System.out.println("title:"+title);*/
        Result result=new Result();
        result.setRes(0);
        Article article = fileUtil.uploadArticle(request, response);
        if (article!=null){
            return articleService.saveArticle(article);
        }else
            result.setMsg("上传出错");
        return result;
    }

    // 播放录像
    @RequestMapping(value="/article/{articleName:.+}", method = RequestMethod.GET)
    public void showVideo(@PathVariable("articleName")String articleName,
                            HttpServletRequest request, HttpServletResponse response){

        fileUtil.showVideo(articleName,request, response);
    }


}
